$(document).ready( function(){
	
	$(".companyContact, .quickFind a").on("click", function(){
		
		var superCategoryID = $("#parent_category_id").val();
		var categoryID = $("#category_id").val();
		var subCategoryID = $("#subcategory_id").val();
		
		var selectedProducts = [];
		
		$("input.productCheck").each( function(){
			if($(this).is(":checked"))
			{
				selectedProducts.push($(this).val());
			}
		});
		
		var dataString = "&what=get_qoute_form&products="+JSON.stringify(selectedProducts);
		
		dataString += "&super_category_id="+superCategoryID+"&category_id="+categoryID+"&subcategory_id="+subCategoryID;
		
		$("#ajaxLoader").show();
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			}).done(function(response){
				if(response){
					$("#qouteModal .modal-body").html(response);
					$("#qouteModal").modal("show");
				}
				$("#ajaxLoader").hide();
			});
		return false;
	});
	
	$("#qouteModal").on("change", "select[name=category_id]", function(){
		var categoryID = $(this).val();
		if(categoryID != 0)
		{
			var dataString = "&what=get_subcategory_options&category_id="+categoryID;
			$("#ajaxLoader").show();
		
			$.ajax({
				type:'POST',
				url: scriptsURL+"libs/ajax/ajax-includes.php",
				data: dataString,
				}).done(function(response){
					if(response){
						$("#qouteModal select[name=subcategory_id]").html(response).attr("disabled", false);
					}
					$("#ajaxLoader").hide();
			});
			return false;
		}
		else
		{
			$("#qouteModal select[name=subcategory_id]").html("").attr("disabled", "disabled");
			
		}
		
		return false;
	});
	
	$("#sendQuote").on("click", function(){
		var dataString = $("#qouteForm").serialize();
		
		dataString += "&what=send_qoute_request";
		$("#ajaxLoader").show();
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			}).done(function(responseJSON){
				
				response = JSON.parse(responseJSON);
				
				var message = "";
				
				if(response.errors)
				{
					$.each(response.errors, function(index, error){
						message = '<div class="">'+error+'</div>';
					});
				}
				else if(response.message)
				{
					message = '<div class="alert alert-success">'+response.message+'</div>';
				}
				
				$("#messageDialog .modal-body").html(message);
				
				$('#messageDialog').modal('show');
				
				$("#ajaxLoader").hide();
				
		});
		return false;
		
	});
	
	$(".productImageThumbs a.thumbImage").on("click", function(){
		var useIMG = $(this).attr("href");
		
		$("#mainImage").attr("src", useIMG);
		
		return false;
	});
	
	$(".productImages a.viewGallery").on("click", function(){
		var images = [];
		$(".productImageThumbs a.thumbImage").each( function(index, element){
			if($(this).attr("href") && $(this).attr("href") != "")
			{
				image = {};
				image["href"] = $(this).attr("href");
				images.push(image);
			}
		});
		$.fancybox.open(images,{padding : 0,
						closeEffect : 'drop',
						nextEffect  : 'elastic',
						prevEffect  : 'elastic',
				        margin     : 0,
				        height   : '100%',
				        width   : '100%',
				        autoCenter : false});
	});
	
	$("#sendCompanyMessage").on("click", function(){
		var dataString = $("#companyContactForm").serialize();
		dataString += "&send_company_contact=1";
		
		$("#ajaxLoader").show();
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			}).done(function(responseJSON){
				
				response = JSON.parse(responseJSON);
				
				var message = "";
				
				if(response.errors)
				{
					$.each(response.errors, function(index, error){
						message = '<div class="">'+error+'</div>';
					});
				}
				else if(response.success)
				{
					message = '<div class="alert alert-success">'+response.success+'</div>';
				}
				
				$("#messageDialog .modal-body").html(message);
				
				$('#messageDialog').modal('show');
				
				$("#ajaxLoader").hide();
			});
			
		return false;
	});
	
	
	
	$("#sortBy ul li").on("click", function(){
		if(!$(this).hasClass("selected"))
		{
			$(this).siblings().removeClass("selected");
			$(this).addClass("selected");
			
			remakeProductsSection();
		}
	});
	$("#priceFliter").on("click", function(){
		remakeProductsSection();
	});
	
	$(".prevSort").on("click", function(){
		var currentOption = $("#sortBy option:selected");
		if(currentOption.index() == 0)
		{
			currentOption.attr("selected", false);
			$("#sortBy option").last().attr("selected", "selected");
			var valueText = $("#sortBy option").last().html();
		}
		else
		{
			var valueText = currentOption.prev().html();
			currentOption.attr("selected", false).prev().attr("selected", "selected");
		}
		$("#sortBy").trigger("change");
		$("#sortHolder .textHolder").html(valueText);
		
		remakeProductsSection();
		
		return false;
	});
	
	$(".nextSort").on("click", function(){
		var currentOption = $("#sortBy option:selected");
		if(currentOption.index() == $("#sortBy option").length)
		{
			currentOption.attr("selected", false);
			$("#sortBy option").first().attr("selected", "selected");
			var valueText = $("#sortBy option").first().html();
		}
		else
		{
			var valueText = currentOption.next().html();
			currentOption.attr("selected", false).next().attr("selected", "selected");
		}
		$("#sortBy").trigger("change");
		$("#sortHolder .textHolder").html(valueText);
		
		remakeProductsSection();
		
		return false;
	});
	
	$(".order.desc").on("click", function(){
		if(!$(this).hasClass("selected"))
		{
			$(this).siblings(".asc").removeClass("selected");
			$(this).addClass("selected");
			$(this).children("span").removeClass("glyphicon-sort-by-alphabet-alt");
			$(this).children("span").addClass("glyphicon-sort-by-alphabet");
			remakeProductsSection();
		}
		return false;
	});
	
	$(".order.asc").on("click", function(){
		if(!$(this).hasClass("selected"))
		{
			$(this).siblings(".desc").removeClass("selected");
			$(this).addClass("selected");
			$(this).children("span").addClass("glyphicon-sort-by-alphabet-alt");
			$(this).children("span").removeClass("glyphicon-sort-by-alphabet");
			remakeProductsSection();
		}
		return false;
	});
	
	
	
	
	$("#multipleColumns").on("click", function(){
		if(!$(this).hasClass("selected"))
		{
			$("#singleColumn").removeClass("selected");
			$(this).addClass("selected");
			
			remakeProductsSection();
		}
		
		return false;
	});
	
	$("#singleColumn").on("click", function(){
		if(!$(this).hasClass("selected"))
		{
			$("#multipleColumns").removeClass("selected");
			$(this).addClass("selected");
			
			remakeProductsSection();
		}
		
		return false;
	});
	
	$(".quickJump button").on("click", function(){
		if($("#page_number").val())
		{
			var page = $("#page_number").val();
			$("#page_no").val(page);
			remakeProductsSection();
		}
		
		return false;
	});
	
	$(".itemsPerPage a").on("click", function(){
		if(!$(this).hasClass("selected"))
		{
			$(this).siblings(".selected").removeClass("selected");
			$(this).addClass("selected");
			remakeProductsSection();
		}
	});
	
	
	$(".rating>div").on("mouseenter",function(){
		var index =$(this).attr('index');
		$(this).parent().children(".hover").removeClass("hover");
		while( index > 0){
			$(this).parent().children("div[index="+index+"]").addClass("hover");
			index = index-1;
		}
	});
	$(".rating").on("mouseleave",function(){
			$(this).find("div").removeClass("hover");
	});

	$(".rating div").on("click",function(){
		
		ratingContainer = $(this).parent();
		
		$("#ajaxLoader").show();
		
		product_id = ratingContainer.attr('product_id');
		rating = $(this).attr("index");
		
		var dataString = "&product_id="+product_id+"&rating="+rating;
		
		
		if(ratingContainer.hasClass("wishmeter"))
		{
			dataString += "&wishmeter=1";
		}
		else
		{
			dataString += "&rate_product=1";
		}
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			}).done(function(responseJSON)
			{
				response = JSON.parse(responseJSON);
				
				$("#ajaxLoader").hide();
				
				if(response.message && response.message != "")
				{
					$("#messageDialog .modal-body").html("<p>"+response.message+"</p>");
					$('#messageDialog').modal('show');
				}
				
				if(response.rating > 0)
				{
					ratingContainer.find('div:lt('+response.rating+')').addClass("bgred");
					ratingContainer.find('div:eq('+response.rating+')').removeClass("bgred");
					ratingContainer.find('div:gt('+response.rating+')').removeClass("bgred");
				}
				
				return false;
		});
	});
});

function remakeProductsSection(){
	
	var sortBy = $("#sortBy").val();
	
	var sortOrder = "asc";
	if($("#sortHolder .order.desc").hasClass("selected"))
	{
		var sortOrder = "desc";
	}
	
	
	var priceFilter = "";
	//var priceFilter = $("#priceFilterValue").val();
	
	var details = 1;
	if($("#multipleColumns").hasClass("selected"))
	{
		details = 0;
	}
	
	
	var companyID = $("#company_id").val(); 
	
	var parentCategoryID = $("#parent_category_id").val();
	var categoryID = $("#category_id").val();
	var subCategoryID = $("#subcategory_id").val();
	
	
	var filters = $("#filters").val();
	var productType = $("#product_type").val();
	
	var page = 1;
	if($("#page_no").val() && $("#page_no").val() > 0)
	{
		var page = $("#page_no").val();
	}
	
	var itemsPerPage = 20;
	
	if($(".itemsPerPage a.selected").length > 0)
	{
		itemsPerPage = $(".itemsPerPage a.selected").attr("items");
	}
	
	
	$("#ajaxLoader").show();
	
	var dataString = "&remake-products=1&sort_by="+sortBy+"&sort_order="+sortOrder+"&details="+details+"&p="+page;
	dataString += "&category_id="+categoryID+"&parent_category_id="+parentCategoryID+"&filters="+filters+"&type="+productType;
	dataString += "&sub_category_id="+subCategoryID+"&company_id="+companyID;
	dataString += "&price_filter="+priceFilter+"&items_per_page="+itemsPerPage;
	
	$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			}).done(function(response)
			{
				if(response != 0)
				{
					if(details == 1)
					{
						$(".productHolder").removeClass("cols-4").addClass("cols-1");
						$(".sortBar .contact, .bottomPager .contact").show();
					}
					else
					{
						$(".productHolder").removeClass("cols-1").addClass("cols-4");
						$(".sortBar .contact, .bottomPager .contact").hide();
					}
					$(".productHolder").html(response);
				}
				
				$("#ajaxLoader").hide();
				
		});
	return false;
}

